/**
 * 树点击节点时触发，刷新content主表格
 */
function reloadGrid() {
	var grid = DSYGrid.getGrid('contentGrid');
	var store = grid.getStore();
	//刷新
	store.load();
};  

var q_id;
var jbxx_edit = false;
Ext.onReady(function() {

   var items = [{
                    xtype: 'button',
                    text: '新增',
                    icon: '/report/image/sysbutton/add.png',
                    handler: function (self) {
                    	jbxx_edit = false;
                    	q_id = GUID.createGUID();
                    	initWindow_report_config().show();
                    }
                },{
                    xtype: 'button',
                    text: '修改',
                    icon: '/report/image/sysbutton/edit.png',
                    handler: function (self) {
                    	var record = DSYGrid.getGrid('contentGrid').getCurrentRecord();
                        if (record == null || record == '' || record == 'undefined') {
                            Ext.MessageBox.alert('提示', '请选择一条数据再进行操作');
                            return;
                        }
                        q_id =  record.get("Q_ID");
                        if(q_id === "dsy_sys_config"){
                        	Ext.toast({
                                html: "系统参数只能查看",
                                closable: false,
                                align: 't',
                                slideInDuration: 400,
                                minWidth: 400
                            });
                         }
                	    Ext.Ajax.request({
                		method : 'POST',
                		url : "/sys-report/getQueryReportInfo",
                		params : {
                			Q_ID:q_id
                		},
                		async : false,
                		success : function(response, options) {
//                			// 获取返回的JSON，并根据gridid，获取表头配置信息
                			var respText = Ext.util.JSON.decode(response.responseText)[0];
                			var REPORT =  eval(respText.REPORT)[0];
                			var REPORT_FIELD =  eval(respText.REPORT_FIELD);
                			var REPORT_CONDITION =  eval(respText.REPORT_CONDITION);
                			jbxx_edit = true;
                			initWindow_report_config().show();
                			loadInfo(REPORT,REPORT_FIELD,REPORT_CONDITION);
                		},
                		failure : function(response, options) {
                			Ext.MessageBox.minWidth = 120;
                			Ext.Msg.alert('提示', '获取报表配置信息失败！');
                		}
                	});
                    }
                },{
                    xtype: 'button',
                    text: '删除',
                    icon: '/report/image/sysbutton/delete.png',
                    handler: function (self) {
                     	// 检验是否选中数据
                      	 var record = DSYGrid.getGrid('contentGrid').getCurrentRecord();
                            if (record==null) {
                                Ext.MessageBox.alert('提示', '请选择至少一条后再进行操作');
                                return;
                            }
                            q_id =  record.get("Q_ID");
                            if(q_id === "dsy_sys_config"){
                            	Ext.MessageBox.alert('提示', '系统参数不允许删除');
                                return;
                            }
                            Ext.Msg.confirm('提示', '请确认是否删除！', function (btn_confirm) {
                                if (btn_confirm == 'yes') {
                                    var Q_ID = record.get("Q_ID");
                                    var Q_NAME = record.get("Q_TITLE");
                                    //发送ajax请求，删除数据
                                    $.post("/sys-report/deleteQueryReport", {
                                    	Q_ID: Q_ID,
                                    	Q_CODE:Q_CODE
                                    }, function (data) {
                                        if (data.success) {
                                            Ext.toast({
                                                html: "删除成功！",
                                                closable: false,
                                                align: 't',
                                                slideInDuration: 400,
                                                minWidth: 400
                                            });
                                            //添加日志
                                            saveLog('删除查询报表','BUTTON',Q_NAME+'删除成功');
                                         // 刷新表格
                                            DSYGrid.getGrid("contentGrid").getStore().loadPage();
                                        } else {
                                        	//添加日志
                                            saveLog('删除查询报表','BUTTON',Q_NAME+'删除失败');
                                            Ext.MessageBox.alert('提示', '删除失败！' + data.message);
                                        }
                                    }, "json");
                                }
                            });
                      }
                },{
                    xtype: 'button',
                    text: '预览',
                    icon: '/report/image/sysbutton/search.png',
                    handler: function (self) {
                    	var record = DSYGrid.getGrid('contentGrid').getCurrentRecord();
                        if (record == null || record == '' || record == 'undefined') {
                            Ext.MessageBox.alert('提示', '请选择一条数据再进行操作');
                            return;
                        }
                        var q_code =  record.get("Q_CODE");
                        var Q_NAME = record.get("Q_TITLE");
                        var yl_url = "/report/page/plat/query/reportQuery.html?code="+q_code+"&adcode="+adcode;
                    	window.open(yl_url, '_blank');
                    	//添加日志
                        saveLog('查询报表预览','BUTTON',Q_NAME+'预览');
                    }
                },{
                    xtype: 'button',
                    text: '导出',
                    icon: '/report/image/sysbutton/export.png',
                    handler: function (self) {
                    	// 获取选中数据
                        var records = DSYGrid.getGrid('contentGrid').getSelection();
                        if (records.length <= 0) {
                            Ext.MessageBox.alert('提示', '请选择至少一条后再进行操作');
                            return;
                        }
                        var qids = [];
                        for (var i in records) {
                        	qids.push(records[i].get("Q_ID"));
                        }
                        DSYGrid.exportExcelClick('contentGrid', {
                            exportExcel: true,
                            url: '/sys-report/ext-form/impQueryReport',
                            param: {
                            	qids:qids
                            }
                        });
                      //添加日志
                        saveLog('导出查询报表','BUTTON');
                    }
                },{
                    xtype: 'button',
                    text: '导入',
                    icon: '/report/image/sysbutton/import.png',
                    handler: function (self) {
                		    var AddfileForm=new Ext.FormPanel(
                		    {
                		        name : 'AddfileForm',
                		        frame : true,
                		        url : '/sys-report/ext-form/impQueryReport',
                		        fileUpload : true,
                		        width : 400,
                		        autoDestroy : true,
                		        items : [{
                		                    xtype : 'filefield',
                		                    emptyText : '选择上传文件',
                		                    fieldLabel : '文件',
                		                    name : 'upload',
                							labelWidth : 40,
//                		                    buttonText : '',
                		                    anchor : '95%',
                							margin:'15 0 0 15',
                							buttonText:'浏览',
                		                    buttonConfig: {
                				                width: 70,
                				                icon: '/report/image/sysbutton/report.png'
                				             },
                		                    validator: function(value){
                								var temp = value.split('.');
												var tmep1 = temp[temp.length-1];
					                            var temp2 = tmep1.toLowerCase();
												var allowType = 'xml';
					                            if (allowType.indexOf(temp2) == -1) {
					                                return "不允许选择该类型文件，请选择xml文件！";
					                            } else {
													return true;
												}
							           }
                		                }]
                		    });
                		    var AddfileWin=new Ext.Window(
                		    {
                	        title: '导入',
                	        width: 500,
                	        height: 150,
                	        layout: 'fit',
                	        plain: true,
                	        buttonAlign: 'center',
                	        items: AddfileForm,
                	        buttons: ['->',{
                	            text: '上传',
                	            handler: function (btn) {
                	            	if (AddfileForm.getForm().isValid()) {
                	            		Ext.MessageBox.show({
                   	            		                       title : '请稍等...',
                   	            		                       msg : '文件上传中...',
                   	            		                       progressText : '',
                   	            		                       width : 300,
                   	            		                       progress : true,
                   	            		                       closable : false,
                   	            		                       animEl : 'loding'
                   	            		                  });
                	            		 AddfileForm.getForm().submit({
                	                        success: function (form, action) {
                	                        	Ext.MessageBox.close();
                	                        	AddfileWin.close();
                	                        	//添加日志
                	                            saveLog('导入查询报表','BUTTON','导入成功');
                	                        	DSYGrid.getGrid("contentGrid").getStore().load();
                	                        },
                	                        failure: function (form, action) {
                	                        	//添加日志
                	                            saveLog('导入查询报表','BUTTON','导入失败');
                	                            var msg = action.result.message;
                	                            Ext.MessageBox.show({
                	                                title: '提示',
                	                                msg: '导入失败:'+msg,
                	                                width: 200,
                	                                fn: function (btn) {
                	                                }
                	                            });
                	                        }
                	                    });
                	            	}
                	            }
                	        }, 
                	           {
                	            text: '关闭',
                	            handler: function () {
                	            	AddfileWin.close();
                	            }
                	        }]
                	    });
                	  AddfileWin.show();
                    }
                }];
    
    var panel=  new Ext.panel.Panel ({
	    renderTo: Ext.getBody(),
	    height:'100%',
        width:'100%',
	    layout: 'fit',
        defaults: {
            split: true,                  //是否有分割线
            collapsible: false           //是否可以折叠
        },
        border: false,
	    dockedItems: [{
			          xtype: 'toolbar',
			          dock: 'top',//bottom
			          itemId: 'contentPanel_toolbar',
			          items: items
	    			}],
        items: [
                initContentGrid()//初始化用户锁定表格
            ]
        
	});
    
    /**
     * 初始化用户锁定表格
     */
    function initContentGrid() {
    	var headerJson = [{
    		"dataIndex" : "Q_ID",
    		"type" : "string",
    		"width" : 200,
    		"text" : "报表编码"
    	},{
    		"dataIndex" : "Q_TITLE",
    		"type" : "string",
    		"width" : 200,
    		"text" : "报表名称"
    	},{
    		"dataIndex" : "Q_TABLE_NAME",
    		"type" : "string",
    		"width" : 200,
    		"text" : "数据集"
    	},{
    		"dataIndex" : "MONEY_UNIT_NAME",
    		"type" : "string",
    		"width" : 80,
    		"text" : "金额单位"
    	},{
    		"dataIndex" : "Q_IS_RIGHT_NAME",
    		"type" : "string",
    		"width" : 100,
    		"text" : "权限过滤"
    	},{
    		"dataIndex" : "Q_IS_PAGE_NAME",
    		"type" : "string",
    		"width" : 80,
    		"text" : "数据分页"
    	},{
    		"dataIndex" : "Q_IS_SUM_NAME",
    		"type" : "string",
    		"width" : 120,
    		"text" : "数据合计行"
    	},{
    		"dataIndex" : "Q_IS_OPENRUN_NAME",
    		"type" : "string",
    		"width" : 120,
    		"text" : "打开即查询"
    	},{
    		"dataIndex" : "Q_IS_BUTTON_NAME",
    		"type" : "string",
    		"width" : 130,
    		"text" : "常用按钮"
    	}];
    	var config = {
                itemId: 'contentGrid',
                border: false,
                flex: 1,
                dataUrl : '/sys-report/getQueryReportList',
                headerConfig: {
                    headerJson: headerJson,
                    columnAutoWidth: false
                },
                checkBox:true,
                pageConfig: {
                    enablePage: false
                },
                rowNumber: {
                    rowNumber: false// 显示行号
                }
    	};
    	var grid = DSYGrid.createGrid(config);
    	return grid;
    }
});
